package com.loongtech.bi.action.birealtime;

import com.loongtech.bi.action.TransferHelper;
import com.loongtech.bi.manager.realTime.UserSecondLeftByHourManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 
 * ClassName: UserSecondLeftByHourBiAction
 * 
 * @Description: TODO
 * @author liuchen
 * @date 2017-5-24 下午7:55:38
 */
@Controller
@Scope("prototype")
@RequestMapping("/realTime/userSecondLeftByHour")
public class UserSecondLeftByHourBiAction extends TransferHelper {
	private static final long serialVersionUID = 1L;
	@Resource
	private UserSecondLeftByHourManager dataManager;

	private int retainDays = 30;

	private List<Object[]> platList = new ArrayList<Object[]>();
	private List<Object[]> chanList = new ArrayList<Object[]>();
	private List<Object[]> servList = new ArrayList<Object[]>();

    @RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String date,
			String callback) {
		try {
			Map<String, Object> result = new HashMap<String, Object>();
			Date lookDate = Time.formatDate(date, "yyyy-MM-dd");

			int d = Time.getInterval(lookDate, new Date());
			if (d > retainDays) {
				result.put("res", "1");
				result.put("msg", "只能查询30天内的数据!");
				printToJson(response, result, callback);
				return;
			}

			platList.clear();
			chanList.clear();
			servList.clear();

			for (Map.Entry<String, Object[]> entry : dataManager.getAllUserSecondLeftByHour(lookDate).entrySet()) {
                Object[] tmp = entry.getValue();
                int tmplength = tmp.length;
			    Object[] tmp1 = new Object[tmplength];
                Object[] tmp2 = new Object[tmplength];
                System.arraycopy(tmp, 0, tmp1, 0, tmplength);
                System.arraycopy(tmp, 0, tmp2, 0, tmplength);
                platList.add(tmp);
				chanList.add(tmp1);
				servList.add(tmp2);
			}
			for (Map.Entry<String, Object[]> entry : dataManager.getPaltform2LeftRealTimeDatas(lookDate).entrySet()) {
				platList.add(entry.getValue());
			}
			for (Map.Entry<String, Object[]> entry : dataManager.getChannel2LeftRealTimeDatas(lookDate).entrySet()) {
				chanList.add(entry.getValue());
			}
			for (Map.Entry<String, Object[]> entry : dataManager.getServer2LeftRealTimeDatas(lookDate).entrySet()) {
				servList.add(entry.getValue());
			}
			for(Object[] o : servList){
				o[0] = BiPlatformManager.getServerNameById((String) o[0]);
			}
			for(Object[] o : platList){
				o[0] = BiPlatformManager.getPlatformNameById((String) o[0]);
			}
			for(Object[] o : chanList){
				o[0] = BiPlatformManager.getChannelNameById((String) o[0]);
			}
			result.put("res", "0");
			result.put("servList", servList);
			result.put("platList", platList);
			result.put("chanList", chanList);
			printToJson(response, result, callback);
		} catch (Exception e) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			e.printStackTrace();
			return;
		}
	}
}
